#!/bin/bash

if [[ `kubectl get pod -n lova-qc --kubeconfig=/root/.kube/config-DQ|wc -l` -eq 0 ]];then
  /root/script/podscale.sh start lova-qc
  sleep 10m
fi
#status=`curl -s -u zhang.zhe:tubaobao520zz -X GET -H "Content-Type: application/json" https://devjira.y3technologies.com/rest/api/2/issue/DEP-68|jq .fields.status.name`

for dep in `tac /root/script/output.txt|awk '{print $1}'`
do
echo "执行工单$dep部署lova-qc环境"
curl -s -u zhang.zhe:tubaobao520zz -X GET -H "Content-Type: application/json" https://devjira.y3technologies.com/rest/api/2/issue/$dep|jq .fields.description >/tmp/zz
hotfix=`curl -s -u zhang.zhe:tubaobao520zz -X GET -H "Content-Type: application/json" https://devjira.y3technologies.com/rest/api/2/issue/$dep|jq .fields.summary|awk '{print $NF}'|awk -F ")" '{print $1}'|awk -F "(" '{print $2}'`
#格式化jira工单的内容格式
sed -i 's!\\r\\n!!g' /tmp/zz
sed -i 's!h2.!\n!g' /tmp/zz
sed -i 's! \*\* !,!g' /tmp/zz
sed -i 's! \* ! !g' /tmp/zz
sed -i 's!, !,!g' /tmp/zz

#配置文件部分
if [[ `cat /tmp/zz|grep Applicaton` ]];then
    echo "有配置文件变更请手动执行"
    exit 0
fi


#数据库部分
echo "hotfix="$hotfix
if [[ $hotfix != "hotfix" ]];then
#############################旧数据库工具暂时不支持##############################################
if [[ `cat /tmp/zz|grep Database` ]];then
    echo "有数据库变更,该脚本暂不支持旧数据库工具，请手动执行"
    exit 0
fi
###############################################################################################
tag=`cat /tmp/zz|grep Database|awk  '{print $2}'|awk -F "," '{print $2}'`
for i in `cat /tmp/zz|grep Database`
do 
  if [[ $i == "Database" || $i =~ "TAG" ]];then
    continue
  fi
  db=`echo $i|awk -F "," '{print $1}'`
  echo "同步$tag到数据库"$db
  #curl -X POST -F token=46937f8948aa0a7126e9d0bca0889e" -F ref="lova-qc" -F variables[SERVICE]=$db -F variables[TARGET]="lova-qc" -F variables[SOURCE]="uat" -F variables[TAG]="$tag" "http://10.202.16.73:8900/api/v4/projects/345/trigger/pipeline"
####################################准生产与生产#######################################################
  #tmp=`echo $i|awk -F "," '{print $2}'`
  #if [[ $tmp == "schema" ]];then
    #svc_schema=`echo $i|awk -F "," '{print $1}'`
    #echo "同步数据库结构"$svc_schema
    #curl -X POST -F token=46937f8948aa0a7126e9d0bca0889e" -F ref="master" -F variables[merge_mode]="SCHEMA" -F variables[SERVICE]=$svc_schema -F variables[TARGET]="lova-qc" -F variables[SOURCE]="lova-test" -F variables[check_environment]="on" "http://10.202.16.73:8900/api/v4/projects/345/trigger/pipeline"
  #elif [[ $tmp == "data" ]];then
    #svc_data=`echo $i|awk -F "," '{print $1}'`
    #echo "同步数据库数据"$svc_data
    #curl -X POST -F token=46937f8948aa0a7126e9d0bca0889e" -F ref="master" -F variables[merge_mode]="DATA" -F variables[SERVICE]=$svc_data -F variables[TARGET]="lova-qc" -F variables[SOURCE]="lova-test" -F variables[check_environment]="on" "http://10.202.16.73:8900/api/v4/projects/345/trigger/pipeline"
  #fi
#################################################################################################################
done
fi

#服务部分
for svc in `cat /tmp/zz|grep Code|awk '{for(i=1;i<=NF;i++)if(i%2==0) print $i}'`
do
   echo "部署服务"$svc
   if [[ $svc == "lova-mobile" ]];then
     curl -X POST -F token=ea0b992ad48a84b569095d80e1272d  -F ref="master" -F variables[env]=lova-qc "http://10.202.16.73:8900/api/v4/projects/89/trigger/pipeline"
   else
     curl -X POST -F token=40afeea2ecef060272d01421a787d1 -F ref="lova-qc" -F variables[SERVICE]=$svc "http://10.202.16.73:8900/api/v4/projects/119/trigger/pipeline"
   fi
done 

while true
do
  if [[ `kubectl get deployments.apps -n lova-qc|awk '{print $2}'|grep 0` ]];then
    echo "部署中...."
  else
    echo "部署完毕...."
    ssh -o "StrictHostKeyChecking no" root@10.202.16.154 "echo "${dep}在lova-qc环境部署完毕"|mail -s "lova-qc环境部署完毕" zhang.zhe@y3.sg,jingjing.mao@y3.sg,sily.zhang@y3.sg,jun.chen@y3.sg"
    #break
    exit 0
  fi
done

#ssh -o "StrictHostKeyChecking no" root@10.202.16.154 "echo "${dep}在lova-qc环境开始部署,请30分钟后尝试使用"|mail -s "lova-qc环境部署完毕" zhang.zhe@y3technologies.com,jingjing.mao@y3technologies.com,sily.zhang@ych.com,jun.chen@ych.com"
done

